home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 4.7 KB | 167 lines | [TEXT/MPS ] |
- // UFailure.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UFAILURE__
- #define __UFAILURE__
-
- // MacApp
-
- #ifndef __MACAPPTYPES__
- #include "MacAppTypes.h"
- #endif
-
- // ANSI
-
- #ifndef __SETJMP__
- #include <setjmp.h>
- #endif
-
- //----------------------------------------------------------------------------------------
- // This macro can be called on any variable to keep it out of a register. It is
- // used specifically in exception handling. Kludge to be used on compilers that
- // don't support volatile. Best solution is to use C++ exception handling so that
- // it becomes the compiler's responsibility to handle register cached variables. But,
- // alas, few compilers support this yet (3/95)
- // If you are only going to compile your source with a compiler that supports volatile,
- // by all means, go ahead and use it (volatile) in your code. But until MacApp requires
- // that all compilers support volatile we have to use it ourselves.
- //----------------------------------------------------------------------------------------
-
- // The new way for functionality
- #define MAVolatile(type, id) type volatile id
- #define MAVolatileInit(type, id, value) type volatile id(value)
-
- //----------------------------------------------------------------------------------------
- // Max and min error number constants
- //----------------------------------------------------------------------------------------
-
- const short minErr = -32768;
- const short maxErr = 32767;
-
-
- //----------------------------------------------------------------------------------------
- // FailInfo
- //----------------------------------------------------------------------------------------
-
- typedef void (*FailureCleanupProc)(void* context);
-
- class FailInfo
- {
- public:
- inline FailInfo();
-
- #if qDebug
- ~FailInfo();
- #endif
-
- FailInfo* Install();
- // returns this
-
- inline void ReSignal();
-
- #if qDebug
- void Success();
- #else
- inline void Success();
- #endif
-
- void Reset();
- // returns this
-
- void SetCleanupProc(FailureCleanupProc theCleanupProc, void* itsContext);
-
- Boolean HandlerExists() const;
- // Returns TRUE if this FailInfo is linked into the failure handling chain
-
- //----------------------------------------------------------------------------------------
- // data members
- //----------------------------------------------------------------------------------------
- public:
- jmp_buf savedState;
- FailInfo* nextInfo;
- FailureCleanupProc cleanupProc;
- void* cleanupContext;
- long message;
- OSErr error;
- #if qDebug
- Boolean installed;
- #endif
-
- //----------------------------------------------------------------------------------------
- // static data members
- //----------------------------------------------------------------------------------------
- public:
- static FailInfo* gTopHandler;
- };
- typedef struct FailInfo *FailInfoPtr;
-
-
- //----------------------------------------------------------------------------------------
- // Global macro definitions
- //----------------------------------------------------------------------------------------
-
- // The Try macro has taken the place of the FailInfo::Try method, for the
- // reason that the code has to _always_ be inline. The decision as to
- // whether or not code is inlined is implemented very differently for various
- // compilers, and can be affected by options such as symbolics generation and
- // level of optimization. Because this decision is so far out of our control,
- // the only real guarantee is to implement Try as a macro…
- //
- // The old way:
- //
- // FailInfo fi;
- // if (fi.Try())
- // {
- // …
- //
- // The new way:
- // FailInfo fi;
- // Try(fi)
- // {
- // …
- //
-
- #define Try(f) if (!setjmp(f.Install()->savedState))
-
- //----------------------------------------------------------------------------------------
- // Global function declarations
- //----------------------------------------------------------------------------------------
-
- void Assertion(const Boolean condition, const CStr255& description);
-
- long BuildMessage(short lowWord, short highWord);
-
- void Failure(OSErr error, long message);
-
- void FailMemError();
-
- void FailResError();
-
- void FailNewMessage(OSErr error, long oldMessage, long newMessage);
-
- void FailNIL(void* p);
-
- void FailNILResource(Handle r);
-
- void FailOSErr(OSErr error);
-
- void FailOSAError(long error);
-
- void ProgramBreak(const CStr255& grievance);
-
- void ProgramReport(const CStr255& grievance, const Boolean breakInDebugger);
-
- //----------------------------------------------------------------------------------------
- // inline member functions
- //----------------------------------------------------------------------------------------
-
- inline FailInfo::FailInfo() { Reset(); }
-
- inline void FailInfo::ReSignal() { ::Failure(error, message); }
-
- #if !qDebug
- inline void FailInfo::Success() { gTopHandler = nextInfo; }
- #endif
-
- #endif
-